package com.xuxueli.commontdemo.commont;
import com.xuxueli.commontdemo.entity.GraduaterName;
import com.xuxueli.commontdemo.entity.GraduaterNamePara;
import com.xuxueli.commontdemo.entity.UserManagerTemp;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Component
public class CommontService {

    /**
     * 单个sheet导入毕业生
     * @param type
     * @param dataList
     */
    @Transactional(rollbackFor = Exception.class)
    public void batchGraduaterData(String delType,String type,List<GraduaterNamePara> dataList) {
        ExecutorService singleThreadExecutor = Executors.newCachedThreadPool();
        List<GraduaterName> list = new ArrayList<>();
        for (GraduaterNamePara excelModel:dataList) {
//            try {
//                FieldValidation.validate(excelModel);
//                GraduaterName graduaterName = new GraduaterName();
//                BeanUtils.copyProperties(excelModel,graduaterName);
//                list.add(graduaterName);
//            } catch (Exception e) {
//                String message = e.getMessage();
//                if(StringUtils.isBlank(message)){
//                    message = e.toString();
//                }
//                excelModel.setMessage(message);
//            }
            GraduaterName graduaterName = new GraduaterName();
            BeanUtils.copyProperties(excelModel,graduaterName);
            if(StringUtils.isNotBlank(type)){
                graduaterName.setType(type);
            }
            if(StringUtils.isNotBlank(graduaterName.getXh())){
                list.add(graduaterName);
            }
        }
        if(CollectionUtils.isNotEmpty(list)){
            singleThreadExecutor.execute(new Runnable() {
                @Override
                public void run() {
//                    GraduaterNameService graduaterNameService = SpringUtil.getBean(GraduaterNameService.class);
//                    EntityWrapper<GraduaterName> wrapper = new EntityWrapper<>();
//                    if("all".equals(delType)){
//                        graduaterNameService.delete(wrapper);
//                        graduaterNameMapper.updataTableAutoIncrement("graduater_name");
//                    }else{
//                        wrapper.eq("type",type);
//                        graduaterNameService.delete(wrapper);
//                    }
//                    graduaterNameService.insertBatch(list,1000);
//                    if("all".equals(delType)){
//                        graduaterNameMapper.updateGraduateType("t_ry_xsjbxxb_s010_d","B");
//                        graduaterNameMapper.updateGraduateType("s016_xj","Y");
//                        graduaterNameMapper.updateGraduateXslb();
//                    }else {
//                        if("B".equals(type)){
//                            graduaterNameMapper.updateGraduateType("t_ry_xsjbxxb_s010_d","B");
//                        }else if("Y".equals(type)){
//                            graduaterNameMapper.updateGraduateType("s016_xj","Y");
//                        }
//                        graduaterNameMapper.updateGraduateXslb();
//                    }
                }
            });
        }
        singleThreadExecutor.shutdown();

    }

    /**
     * 多个sheet导入毕业生信息
     * @param dataList
     * @param tempOwner
     * @param type
     */
    @Transactional(rollbackFor = Exception.class)
    public void importGraduaterMoreSheet(List<UserManagerTemp> dataList, String tempOwner, String type) {
//        userManagerTempService.insertBatch(dataList,1000);
//        userManagerTempMapper.insertGraduaterUser(tempOwner,type);
//        userManagerTempMapper.deleteDataByTempOwner(tempOwner);
    }

    /**
     * 导入角色用户
     * @param dataList
     * @param roleId
     * @param tempOwner
     * @param type
     * @param searchType
     */
    @Transactional(rollbackFor = Exception.class)
    public void importRoleUser(List<UserManagerTemp> dataList, Long roleId, String tempOwner, String type, String searchType) {
//        userManagerTempService.insertBatch(dataList,1000);
//        userManagerTempMapper.insertRoleUser(roleId,tempOwner,type,searchType);
//        userManagerTempMapper.deleteDataByTempOwner(tempOwner);
    }

//    /**
//     * 验证账号操作权限
//     * @param request
//     * @param type
//     */
//    public void checkOperatePersimision(HttpServletRequest request, String type, List<String> list){
//        String userValue = GetUserValue.getUserValue(request);
//        List<Long> roleIdByAccount = sysUserRoleMapper.getRoleIdByAccount(userValue, type);
//        if(CollectionUtils.isNotEmpty(roleIdByAccount)){
//            List<String> menuNameList = sysRoleMenuMapper.getMenuNameList(roleIdByAccount, 1, list);
//            if(CollectionUtils.isEmpty(menuNameList)){
//                throw new TokenException(403,"没有相关的操作权限");
//            }
//        }else {
//            throw new TokenException(403,"没有相关的操作权限");
//        }
//    }

}
